import 'package:top_foundation/top_foundation.dart';

import 'login_post_info.dart';
import 'uias_role_list.dart';
import 'user_org_post_dto_list.dart';

class UserInfo {
  String? userId;
  String? userName;
  String? loginName;
  dynamic loginType;
  dynamic headUrl;
  String? token;
  DateTime? loginTime;
  int? expiresMillisecond;
  String? tenantId;
  String? appId;
  String? appCode;
  int? userType;
  dynamic checkedIdentity;
  List<UiasRoleList>? uiasRoleList;
  dynamic menuIds;
  List<String>? appCodes;
  List<UserOrgPostDtoList>? userOrgPostDtoList;
  dynamic permissionMenus;
  dynamic permissionMenuDtos;
  LoginPostInfo? loginPostInfo;
  dynamic roleVoList;
  String? defaultPlatform;
  String? sessionKey;
  String? openId;
  String? headImgUrl;
  String? nickName;
  String? startTime;
  String? endTime;
  String? gender;
  String get platform {
    if (GetPlatform.isAndroid) {
      return 'flutter-android';
    }
    if (GetPlatform.isIOS) {
      return 'flutter-ios';
    }
    return 'flutter';
  }

  bool get isAdmainRole {
    return roles.contains(UserRole.admin);
  }

  bool get isMainRole {
    return roles.contains(UserRole.main);
  }

  bool get isOrgRole {
    return roles.contains(UserRole.org);
  }

  bool get isPartRole {
    return roles.contains(UserRole.part);
  }

  bool get isNormalRole {
    return roles.contains(UserRole.normal);
  }

  bool get isOtherRole {
    return roles.contains(UserRole.other);
  }

  // 是否报销审批人员
  bool get isReimburseRole {
    return roles.contains(UserRole.reimburse);
  }

  // 是否出差审批人员
  bool get isBusinessRole {
    return roles.contains(UserRole.business);
  }

  // 是否周末值班汇总人员
  bool get isWeekendRole {
    return roles.contains(UserRole.weekend);
  }

  // 是否物品领用审批人员
  bool get isApplyRole {
    return roles.contains(UserRole.apply);
  }

  // 是否报修维修审批人员
  bool get isRepairRole {
    return roles.contains(UserRole.repair);
  }

  Iterable<UserRole> get roles {
    return uiasRoleList?.map((e) => e.role) ?? [];
  }

  UserInfo({
    this.userId,
    this.userName,
    this.loginName,
    this.loginType,
    this.headUrl,
    this.token,
    this.loginTime,
    this.expiresMillisecond,
    this.tenantId,
    this.appId,
    this.appCode,
    this.userType,
    this.checkedIdentity,
    this.uiasRoleList,
    this.menuIds,
    this.appCodes,
    this.userOrgPostDtoList,
    this.permissionMenus,
    this.permissionMenuDtos,
    this.loginPostInfo,
    this.roleVoList,
    this.defaultPlatform,
    this.sessionKey,
    this.openId,
    this.headImgUrl,
    this.nickName,
    this.startTime,
    this.endTime,
    this.gender,
  });

  factory UserInfo.fromJson(Map<String, dynamic> json) {
    var roleList = (json['uiasRoleList'] as List<dynamic>?)
        ?.map((e) => UiasRoleList.fromJson(e as Map<String, dynamic>))
        .toList();

    return UserInfo(
      userId: json['userId'] as String?,
      userName: json['userName'] as String?,
      loginName: json['loginName'] as String?,
      loginType: json['loginType'] as dynamic,
      headUrl: json['headUrl'] as String?,
      token: json['token'] as String?,
      loginTime: json['loginTime'] == null
          ? null
          : DateTime.parse(json['loginTime'] as String),
      expiresMillisecond: json['expiresMillisecond'] as int?,
      tenantId: json['tenantId'] as String?,
      appId: json['appId'] as String?,
      appCode: json['appCode'] as String?,
      userType: json['userType'] as int?,
      checkedIdentity: json['checkedIdentity'] as dynamic,
      uiasRoleList: roleList,
      menuIds: json['menuIds'] as dynamic,
      appCodes: json['appCodes'].cast<String>(),
      userOrgPostDtoList: (json['userOrgPostDTOList'] as List<dynamic>?)
          ?.map((e) => UserOrgPostDtoList.fromJson(e as Map<String, dynamic>))
          .toList(),
      permissionMenus: json['permissionMenus'] as dynamic,
      permissionMenuDtos: json['permissionMenuDTOS'] as dynamic,
      loginPostInfo: json['loginPostInfo'] == null
          ? null
          : LoginPostInfo.fromJson(
              json['loginPostInfo'] as Map<String, dynamic>),
      roleVoList: json['roleVOList'] as dynamic,
      defaultPlatform: json['defaultPlatform'].toString(),
      sessionKey: json['sessionKey'].toString(),
      openId: json['openId'] as String?,
      headImgUrl: json['headImgUrl'] as String?,
      nickName: json['nickName'] as String?,
      startTime: json['startTime'] as String?,
      endTime: json['endTime'] as String?,
      gender: json['gender'] as String?,
    );
  }

  Map<String, dynamic> toJson() => {
        'userId': userId,
        'userName': userName,
        'loginName': loginName,
        'loginType': loginType,
        'headUrl': headUrl,
        'token': token,
        'loginTime': loginTime?.toIso8601String(),
        'expiresMillisecond': expiresMillisecond,
        'tenantId': tenantId,
        'appId': appId,
        'appCode': appCode,
        'userType': userType,
        'checkedIdentity': checkedIdentity,
        'uiasRoleList': uiasRoleList?.map((e) => e.toJson()).toList(),
        'menuIds': menuIds,
        'appCodes': appCodes,
        'userOrgPostDTOList':
            userOrgPostDtoList?.map((e) => e.toJson()).toList(),
        'permissionMenus': permissionMenus,
        'permissionMenuDTOS': permissionMenuDtos,
        'loginPostInfo': loginPostInfo?.toJson(),
        'roleVOList': roleVoList,
        'defaultPlatform': defaultPlatform,
        'sessionKey': sessionKey,
        'openId': openId,
        'headImgUrl': headImgUrl,
        'nickName': nickName,
        'endTime': endTime,
        'startTime': startTime,
        'gender': gender,
        'platform': platform,
      };
}
